package com.ltt.controller;

import com.ltt.common.UserThreadLocal;
import com.ltt.dao.pojo.Group;
import com.ltt.dao.pojo.Plan;
import com.ltt.dao.pojo.User;
import com.ltt.dao.vo.Result;
import com.ltt.mapper.UserInfoMapper;
import com.ltt.service.PlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "学习计划模块")
@RestController
@RequestMapping("/plan")
public class PlanController {

    @Autowired
    private PlanService planService;

    @Autowired
    private UserInfoMapper userInfoMapper;
    @ApiOperation("按时间查询个人计划")
    @PostMapping("/findPlanByTime")
    public Result findPlanByTime(@RequestBody String date) {
        List<Plan> plans = planService.findPlanByTime(date, UserThreadLocal.get().getOpenid());
        return Result.successWithData(plans);
    }

    @ApiOperation("添加个人计划")
    @PostMapping("/setPlan")
    public Result setPlan(@RequestBody Plan plan) {
        plan.setUserId(UserThreadLocal.get().getOpenid());
        boolean res = planService.setPlan(plan);
        return res ? Result.successWithData(res) : Result.failed("添加失败");
    }

    @ApiOperation("打卡")
    @PostMapping("/checkPlan")
    public Result checkPlan(@RequestBody String id) {
        boolean res = planService.checkPlan(id);
        User user = new User();
        user.setOpenid(UserThreadLocal.get().getOpenid());
        if(res){
            userInfoMapper.updateIntegral(user);
        }
        return res ? Result.successWithData(res) : Result.failed("打卡失败");
    }

}
